package com.xiaowen.tool.dto;

import lombok.Data;

import java.io.Serializable;

/**
 * 响应数据
 * @author wwg
 * @since 1.0.0
 */
@Data
public class Result<T> implements Serializable {
    /**
     * 编码：0表示成功，其他值表示失败
     */
    private int code = 0;
    /**
     * 消息内容
     */
    private String msg = "success";

    /**
     * 业务消息内容，需要给用户错误提示但是接口返回成功
     */
    private String errorMsg;

    /**
     * 响应数据
     */
    private T data;

    public Result<T> ok(T data) {
        this.setData(data);
        return this;
    }
    public Result<T> ok(T data,String errorMsg) {
        this.setData(data);
        this.errorMsg = errorMsg;
        return this;
    }

    public boolean success() {
        return code == 0;
    }

    public Result<T> success(String msg) {
        this.msg = msg;
        return this;
    }
    public Result<T> success(int msgCode) {
        this.msg = ErrorCode.getMsg(msgCode);
        return this;
    }
    public Result<T> error() {
        this.code = ErrorCode.INTERNAL_SERVER_VALID_ERROR.getCode();
        this.msg = ErrorCode.INTERNAL_SERVER_VALID_ERROR.getMsg();
        return this;
    }

    public Result<T> error(int code) {
        this.code = code;
        this.msg =ErrorCode.getMsg(code);
        return this;
    }

    public Result<T> error(int code, String msg) {
        this.code = code;
        this.msg = msg;
        return this;
    }

    public Result<T> error(String msg) {
        this.code = ErrorCode.INTERNAL_SERVER_ERROR.getCode();
        this.msg = msg;
        return this;
    }





}
